<?php
/**
 * Created by PhpStorm.
 * User: jksen
 * Date: 2018/1/22
 * Time: 上午9:55
 */

namespace app\common\Model;


class AdminUser extends BaseModel
{
    const PASSWORD_DEFAULT = 'admin@321';

    public static $tab_name = 'admin_user';

    const STATUS_NORMAL = 0;
    const STATUS_FORBID = 1;

    //用户类型 0公司员工 1加盟商
    const USER_TYPE_ADMIN = 0;
    const USER_TYPE_FRANCHISER = 1;

    //用户状态 0正常，1锁定
    public $_status = [
        0=>'正常',
        1=>'禁用',
    ];
    /**
     * 设置AccessToken
     * @param $clientInfo
     * @return int
     */
    protected function setAccessToken($clientInfo)
    {
        //生成令牌
        $accessToken = self::buildAccessToken();
        $accessTokenInfo = [
            'access_token' => $accessToken,//访问令牌
            'expires_time' => time() + Oauth2::$expires,  //过期时间时间戳
            'client' => $clientInfo,//用户信息
        ];
        self::saveAccessToken($accessToken, $accessTokenInfo);
        return $accessTokenInfo;
    }

    /**
     * 生成token
     * @param int $lenght
     * @return bool|string
     */
    protected static function buildAccessToken($lenght = 32)
    {
        $str_pol = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyz";
        return substr(str_shuffle($str_pol), 0, $lenght);
    }

    /**
     * 存储
     * @param $accessToken
     * @param $accessTokenInfo
     */
    protected static function saveAccessToken($accessToken, $accessTokenInfo)
    {
        //存储accessToken
        Cache::set(Oa . $accessToken, $accessTokenInfo, Oauth2::$expires);

        //存储用户与信息索引 用于比较,这里涉及到user_id，如果有需要请关掉注释
        //Cache::set(self::$accessTokenAndClientPrefix . $accessTokenInfo['client']['user_id'], $accessToken, self::$expires);
    }

}